package cn.lin.task.action;

import java.lang.reflect.InvocationTargetException;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.sun.org.apache.commons.beanutils.BeanUtils;

import cn.lin.task.model.Admin;
import cn.lin.task.model.Task;
import cn.lin.task.model.User;
import cn.lin.task.service.IAdminService;
import cn.lin.task.service.ITaskService;
import cn.lin.task.service.IUserService;

@SuppressWarnings("serial")
@Controller("adminAction")
@Scope("prototype")
public class AdminAction extends ActionSupport implements ModelDriven<Admin> {
	
	private Admin admin;
	private IAdminService adminService;
	private ITaskService taskService;
	private IUserService userService;
	private Task task;
	private User user;
	private String msg;
	
	
	
	public IUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}
	
	
	
	public String getMsg() {
		return msg;
	}
	public void setMsg(String msg) {
		this.msg = msg;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}
	public Task getTask() {
		return task;
	}
	public void setTask(Task task) {
		this.task = task;
	}
	public ITaskService getTaskService() {
		return taskService;
	}
	@Resource
	public void setTaskService(ITaskService taskService) {
		this.taskService = taskService;
	}
	public IAdminService getAdminService() {
		return adminService;
	}
	@Resource
	public void setAdminService(IAdminService adminService) {
		this.adminService = adminService;
	}

	public Admin getAdmin() {
		return admin;
	}

	public void setAdmin(Admin admin) {
		this.admin = admin;
	}

	@Override
	public Admin getModel() {
		if(admin==null) admin=new Admin();
		if(task==null) task=new Task();
		if(user==null) user=new User();
		return admin;
	}

	public String loginInput() {
		return SUCCESS;
	}
	
	public void validateLogin() {
		if(admin.getAdminname()==null||admin.getAdminname().equals("")) {
			this.addFieldError("adminname","管理员名不能为空");
		}
		if(admin.getPassword()==null||admin.getPassword().equals("")) {
			this.addFieldError("password", "密码不能为空");
		}
		if(this.hasFieldErrors()) {
			loginInput();
			return;
		}
		Admin dataAdmin=adminService.load(admin.getAdminname());
		this.clearFieldErrors();
		if(dataAdmin==null) {
			this.addFieldError("adminname", "用户不存在");
		}else if(!dataAdmin.getPassword().equals(admin.getPassword())){
			this.addFieldError("password", "密码错误");
		}
		if(this.hasFieldErrors()) {
			loginInput();
		}
	}
	
	public String login() {
		Admin dataAdmin=adminService.load(admin.getAdminname());
		ActionContext.getContext().put("url", "/admin_adminCMD.action?aid="+dataAdmin.getAid()+"&pass="+dataAdmin.getPassword());	
		return "redirect";
		
	}
	
	public boolean passAdminCMD() {
		if(ActionContext.getContext().getParameters().get("aid")==null){
			return false;
		}
		int aid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("aid"))[0]));
		//System.out.println(aid);
		Admin tem=adminService.load(aid);
		//System.out.println(aid);
		if(ActionContext.getContext().getParameters().get("pass")==null) {
			return false;
		}
		String pass=(((String[])ActionContext.getContext().getParameters().get("pass"))[0]);
		//System.out.println(pass);
		if(pass==null) {
			return false;
		}else if(tem==null) {
			return false;
		}else if(!pass.equals(tem.getPassword())) {
			return false;
		}else {
			return true;
		}
	}
	
	public String adminCMD() {
		if(passAdminCMD()) {
		int aid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("aid"))[0]));
		Admin admine=adminService.load(aid);
		ActionContext.getContext().put("adm",admine);
		return SUCCESS;
		}else {
			ActionContext.getContext().put("url", "/admin_loginInput.action");
			return "redirect";
		}
	}
	
	public String updateInput() {
		if(passAdminCMD()) {
		Admin tar= adminService.load(admin.getAid());
		try {
			BeanUtils.copyProperties(admin, tar);
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
		}else {
			ActionContext.getContext().put("url", "/admin_loginInput.action");
			return "redirect";
		}
	}
	
	public String update() {
		adminService.update(admin);
		ActionContext.getContext().put("url", "/admin_adminCMD.action?aid="+admin.getAid()+"&pass="+admin.getPassword());
		return "redirect";
	}
	
	public String addInput() {
		if(passAdd()) {
		String fid=((String[])ActionContext.getContext().getParameters().get("fid"))[0];
		ActionContext.getContext().getValueStack().getContext().put("fid", fid);
		return SUCCESS;
		}else {
			ActionContext.getContext().put("url", "/admin_loginInput.action");
			return "redirect";
		}
	}
	
	public void validateAdd() {
		Admin tem=adminService.load(admin.getAdminname());
		//System.out.println(tem);
		if(tem!=null) {
			this.addFieldError("adminname", "不可以添加相同名字的管理员");
			addInput();
			return;
		}
	}
	
	public boolean passAdd() {
		if(ActionContext.getContext().getParameters().get("fid")==null){
			return false;
		}
		int aid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("fid"))[0]));
		Admin tem=adminService.load(aid);
		System.out.println(aid);
		if(ActionContext.getContext().getParameters().get("pass")==null) {
			return false;
		}
		String pass=(((String[])ActionContext.getContext().getParameters().get("pass"))[0]);
		//System.out.println(pass);
		if(pass==null) {
			return false;
		}else if(tem==null) {
			return false;
		}else if(!pass.equals(tem.getPassword())) {
			return false;
		}else {
			return true;
		}
	}
	
	public String add() {	
		if(ActionContext.getContext().getParameters().get("fid")==null){
			return "error";
		}
		int aid=Integer.parseInt((((String[])ActionContext.getContext().getParameters().get("fid"))[0]));
		ActionContext.getContext().put("fadmin",adminService.load(aid));
		adminService.add(admin);
		return SUCCESS;
	}
	
	public String adminlist() {
		ActionContext.getContext().put("adminlist", adminService.find());
		return SUCCESS;
	}
	
	public String tasklist() {
	
			ActionContext.getContext().put("tl", taskService.find());
			//System.out.println(taskService.find().getDatas());
			return SUCCESS;
	
			
		
	}
	
	public String deletetask() {
		try {
			int tid=Integer.parseInt(ServletActionContext.getRequest().getParameter("tid"));
			int aid=Integer.parseInt(ServletActionContext.getRequest().getParameter("aid"));
			String pass=ServletActionContext.getRequest().getParameter("pass");
			taskService.delete(tid);
			ActionContext.getContext().put("url", "/admin_tasklist.action?aid="+aid+"&pass="+pass);
			return "redirect";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	
	public String updatetaskInput() {
		try {
			int tid=Integer.parseInt(ServletActionContext.getRequest().getParameter("tid"));
			//System.out.println("tid="+tid);
			Task tar=taskService.load(tid);
			task.setTid(tar.getTid());
			task.setTaskname(tar.getTaskname());
			task.setContext(tar.getContext());
			task.setCreateDate(tar.getCreateDate());
			task.setLastDate(tar.getLastDate());
			task.setUser(tar.getUser());
			task.setIsFinish(tar.getIsFinish());
			ActionContext.getContext().put("ul", userService.listAllUser());
			return SUCCESS;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
	}
	
	
	public String updatetask() {
		try {
//			System.out.println(task);
//			System.out.println(task.getCreateDate());
			task.setUser(userService.load(task.getUser().getUid()));
			int aid=admin.getAid();
			//System.out.println(task);
			taskService.update(task);
			ActionContext.getContext().put("url", "/admin_tasklist.action?aid="+aid+"&pass="+admin.getPassword());
			return "redirect";
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "error";
		}
		//taskService.update(task);
	}
	
	public String addtaskInput() {
		try {
			ActionContext.getContext().put("ul", userService.listAllUser());
			return SUCCESS;
		}catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			return  "error";
		}
	}
	
	public String addtask() {
		task.setUser(userService.load(task.getUser().getUid()));
		int aid=admin.getAid();
		taskService.add(task);
		System.out.println("aid "+admin.getAid()+"pass "+admin.getPassword());
		ActionContext.getContext().put("url", "/admin_tasklist.action?aid="+aid+"&pass="+admin.getPassword());
		return "redirect";
	}
	
	public String userlist() {
//		System.out.println(admin.getAid());
        System.out.println(ActionContext.getContext().get("ul"));
		
		ActionContext.getContext().put("ul", userService.find());
		//System.out.println(taskService.find().getDatas());
		return SUCCESS;
	}
	
	public String userinfo() {
//		System.out.println(user.getUid());
//		System.out.println(admin.getAid());
//		System.out.println(admin.getPassword());
		try {
			BeanUtils.copyProperties(user, userService.load(user.getUid()));
			ActionContext.getContext().put("utl", taskService.find(user));
		} catch (IllegalAccessException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (InvocationTargetException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return SUCCESS;
	}
	
	public String search() {
		System.out.println(admin.getAid());
		System.out.println(admin.getPassword());
		Set<User> users=new HashSet<>();
		List<User> gra=userService.list("from User user where user.grade like '%"+msg+"%'");
		List<User> clz=userService.list("from User user where user.clazzroom like '%"+msg+"%'");
		List<User> name=userService.list("from User user where user.username like '%"+msg+"%'");
		List<User> stuID=userService.list("from User user where user.stuID like '%"+msg+"%'");
		List<User> intitu=userService.list("from User user where user.institute like '%"+msg+"%'");
		List<User> major=userService.list("from User user where user.major like '%"+msg+"%'");
		users.addAll(gra);
		users.addAll(clz);
		users.addAll(name);
		users.addAll(stuID);
		users.addAll(intitu);
		users.addAll(major);
		ActionContext.getContext().put("ul", users);
		ActionContext.getContext().put("aid", admin.getAid());
		ActionContext.getContext().put("password", admin.getPassword());
		return SUCCESS;
	}
	
	
}
